<template>
  <div>
    <!-- 校验的三个要素
     el-form model 绑定数据对象 rules 绑定规则对象
     el-form-item prop 绑定字段名字符串-->
    <el-form :model="formData" :rules="rules">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" />
      </el-form-item>
      <el-form-item label="密码" prop="checkPassword">
        <el-input v-model="formData.checkPassword" />
      </el-form-item>
      <el-form-item>
        <el-button>登录</el-button>
      </el-form-item>
    </el-form>
    <!-- 也可以用 :props="{label: 'name'}" default-expand-all -->
    <el-tree :data="treeArr" default-expand-all>
      <template #default="obj">
        {{ obj.data.name }}
        负责人{{ obj.data.manager }}
      </template>
    </el-tree>
  </div>
</template>

<script>
export default {
  data() {
    // 自定义校验函数
    const checkPassword = (rule, val, callback) => {
      // 1.编写自定义校验逻辑 得出true/false
      const isValid = val === this.formData.password
      //   2.根据element ui 的规定调用callback
      if (isValid) {
        // 成功
        callback()
      } else {
        // 失败
        callback(new Error('两次密码必须一致'))
      }
    }
    return {
      formData: {
        mobile: '',
        password: '',
        checkPassword: ''
      },
      rules: {
        mobile: [
          { required: true, message: '输入不能为空', trigger: 'blur' },
          { pattern: /^1[3-9][0-9]{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '输入不能为空', trigger: 'blur' },
          { max: 16, min: 6, message: '长度在6-16之间', trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, message: '输入不能为空', trigger: 'blur' },
          { validator: checkPassword, trigger: 'blur' }
        ]
      },
      treeArr: [
        { name: '总裁办', manager: '小李黑丝' },
        { name: '财务处', manager: '小李黑丝' },
        { name: '技术部', manager: '小李黑丝' },
        { name: '黑丝管理处',
          manager: '小李黑丝',
          children: [{
            name: '黑丝管理员',
            manager: '小李黑丝'
          }] }
      ]
    }
  },
  created() {
    this.$store.dispatch('user/getUserInfo')
  }
}
</script>

<style>
a{
  text-align: left;
}
</style>
